package com.instalinks.android.lib;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.instalinks.android.lib.enums.ExternalActivityType;

public class ApplicationNotifications {

	private String mNotificationAppTitle;

	private Context mContext;
	private int mNotificationId;
	private int mIcon;
	private boolean mAllowSound;
	private boolean mAutoClear;
	private String mActivityClassName;

	private NotificationManager mNotificationManager;

	public ApplicationNotifications(Context context, int notificationId,
			String notificationAppTitle, int icon, boolean allowSound,
			boolean autoClear) {
		
		initialize(context, notificationId,
				notificationAppTitle, icon, allowSound,
				autoClear, "");
	}

	public ApplicationNotifications(Context context, int notificationId,
			String notificationAppTitle, int icon, boolean allowSound,
			boolean autoClear, String activityClassName) {
		
		initialize(context, notificationId,
				notificationAppTitle, icon, allowSound,
				autoClear, activityClassName);
		
	}
	
	private void initialize(Context context, int notificationId,
			String notificationAppTitle, int icon, boolean allowSound,
			boolean autoClear, String activityClassName){
		
		this.mContext = context;
		this.mNotificationId = notificationId;
		this.mIcon = icon;
		this.mNotificationAppTitle = notificationAppTitle;
		this.mAllowSound = allowSound;
		this.mAutoClear = autoClear;
		this.mActivityClassName = activityClassName;

		mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		
	}

	public void notifyClearAll() {
		mNotificationManager.cancel(this.mNotificationId);
	}

	public void notify(String msg) {
		notify(msg, this.mAllowSound);
	}

	public void notify(String msg, String activityName) {
		notify(msg, mAllowSound, mAutoClear, mIcon, activityName);
	}

	public void notify(String msg, boolean autoClear, int icon) {
		notify(msg, mAllowSound, autoClear, icon);
	}

	public void notify(String msg, boolean allowSound) {
		notify(msg, allowSound, this.mAutoClear, this.mIcon);
	}

	public void notify(String msg, boolean allowSound, boolean autoClear,
			int icon) {
		notify(msg, allowSound, autoClear, icon, this.mActivityClassName);
	}

	public void notify(String msg, boolean allowSound, boolean autoClear,
			int icon, String activityName) {

		long when = System.currentTimeMillis();

		Notification notification = new Notification();
		notification.tickerText = msg;
		notification.when = when;
		notification.icon = icon;

		notification.flags = (autoClear ? Notification.FLAG_AUTO_CANCEL
				: Notification.FLAG_ONGOING_EVENT);

		if (mAllowSound && allowSound)
			notification.defaults |= Notification.DEFAULT_SOUND;

		Intent notificationIntent = null;
		if (activityName.length() > 0) {
			notificationIntent = new Intent(Intent.ACTION_MAIN);

			if (activityName.startsWith("com.instalinks.android.")) {
				notificationIntent.setClassName(mContext, activityName);
			} else if (activityName.toLowerCase().equals(
					ExternalActivityType.MessageBox.toString().toLowerCase())) {
				notificationIntent.setType("vnd.android-dir/mms-sms");
			}
		}

		PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(mContext, this.mNotificationAppTitle,
				msg, contentIntent);

		mNotificationManager.notify(this.mNotificationId, notification);
	}
	// **********************//

}
